generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int        @id @default(autoincrement())
  createdAt DateTime   @default(now()) @map("created_at")
  updatedAt DateTime   @updatedAt @map("updated_at")
  email     String     @unique
  password  String
  isAdmin   Boolean    @default(false) @map("is_admin")
  phone     String?    @unique
  name      String     @default("")
  feedbacks Feedback[]
  orders    Order[]
}

model Product {
  id          Int         @id @default(autoincrement())
  createdAt   DateTime    @default(now()) @map("created_at")
  updatedAt   DateTime    @updatedAt @map("updated_at")
  name        String      @unique
  slug        String      @unique
  description String
  price       Int
  image       String
  weight      Int
  categoryId  Int?        @map("category_id")
  orderItems  OrderItem[]
  category    Category?   @relation(fields: [categoryId], references: [id])
}

model Category {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")
  name      String    @unique
  slug      String    @unique
  products  Product[]
}

model Order {
  id        Int             @id @default(autoincrement())
  createdAt DateTime        @default(now()) @map("created_at")
  updatedAt DateTime        @updatedAt @map("updated_at")
  status    EnumOrderStatus @default(PENDING)
  address   String
  commentary   String?      @default("нет")
  total     Int
  deliveryDate String
  deliveryTime String
  userId    Int             @map("user_id")
  user      User            @relation(fields: [userId], references: [id])
  items     OrderItem[]
}

model OrderItem {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")
  quantity  Int
  price     Int
  orderId   Int?     @map("order_id")
  productId Int      @map("product_id")
  order     Order?   @relation(fields: [orderId], references: [id])
  product   Product  @relation(fields: [productId], references: [id])

  @@map("Order_item")
}

model Feedback {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")
  text      String
  userId    Int      @map("user_id")
  user      User     @relation(fields: [userId], references: [id])
}

enum EnumOrderStatus {
  PENDING
  PAYED
  SHIPPED
  DELIVERED
  CANCELED
}
